package com.vancouver.knows.ui.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.EmailSendSecurityCodeTaskMark;
import com.vancouver.knows.business.task.mark.EmailUserRegisterTaskMark;
import com.vancouver.knows.business.task.mark.PhoneSendSecurityCodeTaskMark;

public class RegisterActivity extends BaseActivity implements IResultReceiver{

	private Context mContext;
	@Bind(R.id.common_tips)
	TextView  mCommonTips;
	@Bind(R.id.common_back)
	ImageView mCommonBack;
	@Bind(R.id.btn_register)
	Button mRegisterBtn;
	@Bind(R.id.code)
	MyAutoCompleteTextView mSecurityCode;
	@Bind(R.id.nick_name)
	MyAutoCompleteTextView mNickName;
	@Bind(R.id.psd)
	MyAutoCompleteTextView mPassword;
	@Bind(R.id.get_again)
	TextView getTime;
	
	private String mRegisterCount;
	private String mFromJump;
	private String mPhoneAreaCode;
	private int totalSecond = 120;
	private LoadingDialog loadingDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_register);
		mContext = RegisterActivity.this;
		setViews();
		setListeners();
	}

	private void setViews() {
		loadingDialog = new LoadingDialog(mContext);
		Intent intent = getIntent();
		String tips = intent.getStringExtra("COMMON_TIPS");
		mRegisterCount =  intent.getStringExtra("REGISTER_COUNT");
		mFromJump = intent.getStringExtra("FROM_JUMP");
		try {
			mPhoneAreaCode = intent.getStringExtra("AREA_CODE");
		} catch (Exception e) {
			e.printStackTrace();
		}
		mCommonTips.setText(tips);
		handler.postDelayed(runnable, 1000);
	}
	
	private void setListeners(){
		mCommonBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@OnClick(R.id.btn_register)
	public void onRegisterBtnClick(){
		if (mSecurityCode.getText().toString().trim().equals("")
					||mNickName.getText().toString().trim().equals("")
					||mPassword.getText().toString().trim().equals("") ) {
			Toast.makeText(mContext, mContext.getString(R.string.register_tips), Toast.LENGTH_SHORT).show();
			return;
		}
			
		BusinessModule bModule = imContext.getBusinessModule();
		EmailUserRegisterTaskMark taskMark = bModule.getTaskMarkPool().getEmailUserRegisterTaskMarkList();
		if (mFromJump.equals(PhoConstants.CURRENT_OPEN_FROM_REGISTER_EMAIL)){
			//发送登录需要的参数
			bModule.getServiceWrapper().userEmailRegister(RegisterActivity.this, taskMark
					, mRegisterCount
					, mSecurityCode.getText().toString().trim()
					, mNickName.getText().toString().trim()
					, mPassword.getText().toString().trim());
		} else if(mFromJump.equals(PhoConstants.CURRENT_OPEN_FROM_REGISTER_PHONE)){
			//发送登录需要的参数
			bModule.getServiceWrapper().userPhoneRegister(RegisterActivity.this, taskMark
					, mRegisterCount
					, mSecurityCode.getText().toString().trim()
					, mNickName.getText().toString().trim()
					, mPassword.getText().toString().trim()); 
		}
		if (loadingDialog != null)
			loadingDialog.showLoadingDialog(mContext.getString(R.string.register_loading));
		
	}
	
	@OnClick(R.id.get_again)
	public void onRegisterAgainClick(){
		BusinessModule bModule = imContext.getBusinessModule();
		//重新获取验证码
		if (mFromJump.equals(PhoConstants.CURRENT_OPEN_FROM_REGISTER_EMAIL)){
			EmailSendSecurityCodeTaskMark taskMark = bModule.getTaskMarkPool().getEmailSendSecurityCodeTaskMarkList();
			//发送登录需要的参数
			bModule.getServiceWrapper().userEmailSendSecurityCode(RegisterActivity.this, taskMark, mRegisterCount);
		} else if(mFromJump.equals(PhoConstants.CURRENT_OPEN_FROM_REGISTER_PHONE)){
			PhoneSendSecurityCodeTaskMark taskMark = bModule.getTaskMarkPool().getPhoneSendSecurityCodeTaskMarkList();
			//发送登录需要的参数
			bModule.getServiceWrapper().userPhoneSendSecurityCode(RegisterActivity.this, taskMark, mRegisterCount);
		}
		if (loadingDialog != null)
			loadingDialog.showLoadingDialog(mContext.getString(R.string.send_code_loading));
	}
	
	public Handler handler = new Handler(); 
	/**
	 * 定时器
	 */
    Runnable runnable = new Runnable() { 
        @Override 
        public void run() { 
        	if (totalSecond != 0){
	        	totalSecond--; 
	            handler.postDelayed(this, 1000); 
	            getTime.setText(imContext.getString(R.string.get_again, totalSecond)); 
        	}else{
        		totalSecond = 0;
        		getTime.setText(imContext.getString(R.string.re_again)); 
        	}
        } 
    }; 
    
    @Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadingDialog != null){
			loadingDialog.dismiss();
		}
    }

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if (loadingDialog != null)
			loadingDialog.closeLoadingDialog();
		if (exception != null && !exception.getExMessage().equals("")){
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_SHORT).show();
		}else{
			if (taskMark instanceof EmailUserRegisterTaskMark && taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER){
				Toast.makeText(mContext, mContext.getString(R.string.register_success), Toast.LENGTH_SHORT).show();
				finish();
			}else if (((taskMark instanceof PhoneSendSecurityCodeTaskMark) || (taskMark instanceof EmailSendSecurityCodeTaskMark))
					&& taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
				totalSecond = 120;
				handler.postDelayed(runnable, 1000);
			}
		}
		
	}

}